import React, { Component, Fragment } from 'react';
import { WeaProgress, WeaDialog,WeaTools } from 'ecCom';
import { Button, Progress, message } from 'antd';


export default class ProgressComponent extends Component {
    constructor(props) {
        super(props);
        this.requestid = -1;
        this.docid = -1;
        this.param = '';
        this.oldrequestid = -1;
        this.state = {
            count: 1,
            visible: true,
            uploading:true,
            allnumber:0,
            successNumber:0

        }
    }

    componentDidMount() {
        this.add();
        this.dowriteNumberToFile();
    }


    add = () => {
        const conut = this.state.count;
        this.setState({ count: conut + 2 });
        const timer = setTimeout(() => {
            this.add()
        }, 1000);
        if (this.state.count >= 99) {
            clearTimeout(timer);

        }
    };

    dowriteNumberToFile=()=> {
        const progressComponent = document.getElementById('progressComponent');

        if (progressComponent.getAttribute('requestid')) {
            this.requestid = progressComponent.getAttribute('requestid');
        }
        if (progressComponent.getAttribute('docid')) {
            this.docid = progressComponent.getAttribute('docid');
        }
        if (progressComponent.getAttribute('param')) {
            this.param = progressComponent.getAttribute('param');
        }
        if (progressComponent.getAttribute('oldrequestid')) {
            this.oldrequestid = progressComponent.getAttribute('oldrequestid');
        }
        const data=   {
              requestid: this.requestid,
                docid: this.docid,
                otherParam: this.param,
            oldrequestid:this.oldrequestid
        };
        WeaTools.callApi('/api/odoc/odocWorkflow/writeNumberToFile', 'POST', data).then((data) => {
            this.setState({
                count: 100,
                uploading:false
            })
            if(data.api_status){
                this.setState({
                    allnumber: data.allCount,
                    successNumber:data.successConut
                })
                let i=0;
                data.data.map(item =>{
                  window.setDetailCl(i,item.status,item.Number);
                    i++;
                })
            }
            WfForm.doSave();
        });

    }

    getButtons = () => {
        let buttons = []


            buttons = [
                <Button type="primary" onClick={() => {
                    this.createNumber()
                }}>重新生成</Button>,
                <Button type="primary" onClick={() => { this.removeDialog() }}>关闭</Button>
            ]

        return buttons

    }



removeDialog = () => {

    this.setState({
        visible: false
    })
    const progressComponent = document.getElementById('progressComponent');
    progressComponent.parentNode.removeChild(progressComponent);
}
    createNumber=()=>{
        this.setState({
            uploading: true,
            count:1
        })
        this.add();
        this.dowriteNumberToFile();

    }
render() {

    return (
        <div id="progressComponent">
            <WeaDialog
                title="份号生成中......."
                icon="icon-coms-official"
                visible={this.state.visible}
                style={{ width: 450, height: 100 }}
                parentClassName="progressComponent"
                buttons={this.state.uploading&&this.getButtons()}
                closable={false}
            >
                <div className="processInfo" style={{textAlign:'center',padding:'20px',fontSize:'16px'}}>
                    {!this.state.uploading&&`一共${this.state.allnumber}份!成功${this.state.successNumber}份!`}
                {this.state.uploading&&<Progress  percent={this.state.count} strokeColor="#B37BFA" />}
                    </div>
            </WeaDialog>
        </div>

    );

}}